package code.lewdy.timer;

import java.util.Calendar;
import java.util.Timer;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;

/**
 * 继承HttpServlet,并在web.xml中作如下配置:
    <listener>
        <listener-class>code.lewdy.timer.TimerTaskServlet</listener-class>
    </listener>

 * @author liuding,2012-4-16
 *
 * $LastChangedDate$
 * $Author$ 
 */
public class TimerTaskServlet extends HttpServlet implements ServletContextListener
{
    private Timer timer = null;

    private Calendar cal = Calendar.getInstance();

    public void contextInitialized(ServletContextEvent arg0)
    {
        timer = new java.util.Timer(true);
        System.out.println(cal.getTime() + "定时器已启动");
        timer.schedule(new TimerBusiness(), 0, 10* 1000);
        System.out.println(cal.getTime() + "已经添加任务调度表");
    }

    public void contextDestroyed(ServletContextEvent arg0)
    {
        timer.cancel();
        System.out.println(cal.getTime() + "定时器销毁");
    }

}
